package com.jianbing.north.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jianbing.common.utils.SecurityUtils;
import com.jianbing.north.domain.NorthUserInfo;
import com.jianbing.north.domain.NorthUserRecordDTO;
import com.jianbing.north.service.INorthUserInfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.jianbing.common.annotation.Log;
import com.jianbing.common.core.controller.BaseController;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.enums.BusinessType;
import com.jianbing.north.domain.NorthAnswerRecord;
import com.jianbing.north.service.INorthAnswerRecordService;
import com.jianbing.common.utils.poi.ExcelUtil;
import com.jianbing.common.core.page.TableDataInfo;

/**
 * 用户测试结果Controller
 * 
 * @author ruoyi
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/north/record")
public class NorthAnswerRecordController extends BaseController
{
    @Autowired
    private INorthAnswerRecordService northAnswerRecordService;
    @Autowired
    private INorthUserInfoService northUserInfoService;
    /**
     * 查询用户测试结果列表
     */
    //  @PreAuthorize("@ss.hasPermi('north:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(NorthAnswerRecord northAnswerRecord)
    {
        startPage();
        northAnswerRecord.setUserId(SecurityUtils.getUserId());
        List<NorthAnswerRecord> list = northAnswerRecordService.selectNorthAnswerRecordList(northAnswerRecord);
        return getDataTable(list);
    }

    /**
     * 导出用户测试结果列表
     */
    //  @PreAuthorize("@ss.hasPermi('north:record:export')")
    @Log(title = "用户测试结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NorthAnswerRecord northAnswerRecord)
    {
        List<NorthAnswerRecord> list = northAnswerRecordService.selectNorthAnswerRecordList(northAnswerRecord);
        ExcelUtil<NorthAnswerRecord> util = new ExcelUtil<NorthAnswerRecord>(NorthAnswerRecord.class);
        util.exportExcel(response, list, "用户测试结果数据");
    }

    /**
     * 获取用户测试结果详细信息
     */
    //  @PreAuthorize("@ss.hasPermi('north:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(northAnswerRecordService.selectNorthAnswerRecordById(id));
    }

    /**
     * 新增用户测试结果
     */
    //  @PreAuthorize("@ss.hasPermi('north:record:add')")
    @Log(title = "用户测试结果", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody NorthAnswerRecord northAnswerRecord)
    {
        northAnswerRecord.setUserId(SecurityUtils.getUserId());
        northAnswerRecord.setInsertTime(new Date());
        Long part = northAnswerRecord.getPart();
        Long fraction = northUserInfoService.getFraction(northAnswerRecord.getUserId(), part.intValue());
        if(fraction<0){

        }else{
            NorthUserInfo northUserInfo = new NorthUserInfo();
            northUserInfo.setUserId(northAnswerRecord.getUserId());
            Long newFraction = fraction+northAnswerRecord.getFraction()<0?0:fraction+northAnswerRecord.getFraction();
            if(part==1){
                northUserInfo.setP1Fraction(newFraction);
            }else if(part==2){
                northUserInfo.setP2Fraction(newFraction);
            }
            northUserInfoService.updateNorthUserInfo(northUserInfo);
        }
        return toAjax(northAnswerRecordService.insertNorthAnswerRecord(northAnswerRecord));
    }

    /**
     * 修改用户测试结果
     */
    //  @PreAuthorize("@ss.hasPermi('north:record:edit')")
    @Log(title = "用户测试结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NorthAnswerRecord northAnswerRecord)
    {
        return toAjax(northAnswerRecordService.updateNorthAnswerRecord(northAnswerRecord));
    }

    /**
     * 删除用户测试结果
     */
    //   @PreAuthorize("@ss.hasPermi('north:record:remove')")
    @Log(title = "用户测试结果", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(northAnswerRecordService.deleteNorthAnswerRecordByIds(ids));
    }

    /**
     * 获取用户答题记录
     *
    * @param part
     * @return com.jianbing.common.core.domain.AjaxResult
     * @author zheng
     * @create 2024/6/23
     **/

    // @PreAuthorize("@ss.hasPermi('north:record:list')")
    @GetMapping(value = "/summary")
    public TableDataInfo getSummary(@Param("part") Long part)
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<NorthUserRecordDTO> list = northAnswerRecordService.selectNorthAnswerRecordSummaryList(userId,part);
        return getDataTable(list);
    }

    //  @PreAuthorize("@ss.hasPermi('north:record:query')")
    @GetMapping("/fraction")
    public AjaxResult getFraction(@RequestParam(required = false)Integer part)
    {
        Long userId = SecurityUtils.getUserId();
        return success(northUserInfoService.getFraction(userId,part));
    }
}
